﻿@page "/mod"
@using Flagrum.Core.Archive
@using Flagrum.Web.Features.WorkshopMods.Components

@inject AppStateService AppState
@inject BinmodTypeHelper BinmodTypeHelper
@inject ModelReplacementPresets ModelReplacementPresets
@inject IStringLocalizer<Index> L

<LoadingView Text="Loading Mods"
             IsLoading="IsLoading">
    <div class="flex flex-row items-start px-7 pb-5 border-b border-t border-dark-600 sticky top-0 z-40 bg-dark">
        <div class="flex flex-row flex-wrap flex-grow">
            <div class="bg-dark-600 shadow-md rounded row mt-5 mr-10">
                <ModTypeButtonGroup Target="AppState.ActiveCategoryFilter" OnItemSelected="FilterCategory">
                    <ModTypeButton Text="@L["LocalMods"]" Value="0"/>
                    <ModTypeButton Text="@L["WorkshopMods"]" Value="1"/>
                    <ModTypeButton Text="@L["AllMods"]" Value="-1"/>
                </ModTypeButtonGroup>
            </div>
            <div class="bg-dark-600 shadow-md rounded row mt-5 mr-6">
                <ModTypeButtonGroup Target="AppState.ActiveModTypeFilter" OnItemSelected="FilterType">
                    <ModTypeButton Text="@L["AllMods"]" Value="-1"/>
                    <ModTypeButton Text="@L["Outfits"]" Value="(int)BinmodType.Cloth"/>
                    <ModTypeButton Text="@L["Weapons"]" Value="(int)BinmodType.Weapon"/>
                    <ModTypeButton Text="@L["Replacements"]" Value="(int)BinmodType.Character"/>
                    <ModTypeButton Text="@L["ComradesStyles"]" Value="(int)BinmodType.StyleEdit"/>
                    <ModTypeButton Text="@L["ComradesWeapons"]" Value="(int)BinmodType.Multi_Weapon"/>
                </ModTypeButtonGroup>
            </div>
            <div class="flex-grow"></div>
            <div class="bg-dark-600 shadow-md rounded row mt-5 px-4">
                <span class="text-accent1-200 font-display text-lg">
                    @AppState.Mods.Count(m => (AppState.ActiveCategoryFilter == -1 || m.IsWorkshopMod == (AppState.ActiveCategoryFilter == 1)) && (AppState.ActiveModTypeFilter == -1 || m.Type == AppState.ActiveModTypeFilter))
                    &nbsp;&nbsp;/&nbsp;&nbsp;@AppState.Mods.Count(m => AppState.ActiveModTypeFilter == -1 || m.Type == AppState.ActiveModTypeFilter)&nbsp;&nbsp;/&nbsp;&nbsp;@Limits[AppState.ActiveModTypeFilter]
                </span>
            </div>
        </div>
    </div>

    <div class="flex flex-row" style="height: calc(100% - 74px)">
        <div class="flex-grow pt-2">
            <div class="row flex-wrap px-4 ml-0.5 mb-8">
                <CascadingValue Value="this">
                    <NewWorkshopModCard/>
                    <WorkshopModCardGroup Mods="AppState.Mods.Where(m => (AppState.ActiveCategoryFilter == -1 || m.IsWorkshopMod == (AppState.ActiveCategoryFilter == 1)) && (AppState.ActiveModTypeFilter == -1 || m.Type == AppState.ActiveModTypeFilter)).OrderByDescending(m => m.IsApplyToGame).ThenBy(m => m.Type).ThenBy(m => m.Target).ThenBy(m => m.GameMenuTitle ?? m.WorkshopTitle)"/>
                </CascadingValue>
            </div>
        </div>
        <div class="h-full py-6 pr-5 pl-2" style="flex: 0 0 242px;">
            @foreach (var (modType, limit) in Limits)
            {
                if (modType > -1)
                {
                    if (AppState.Mods.Count(m => m.Type == modType) > limit)
                    {
                        <div class="rounded-md ml-2 px-3 py-2 mb-4" style="background: #440000; color: #ff0000; border: 1px solid #ff0000">
                            Limit exceeded for<br/>
                            @BinmodTypeHelper.GetDisplayName((BinmodType)modType) mods.
                        </div>
                    }
                }
            }
            <strong class="font-display block ml-2 text-grey-300 mb-3" style="font-size: @(CultureInfo.CurrentCulture.Name == "ja-JP" ? 0.9 : 1.2)rem">@L["Browse"]</strong>
            <small class="block text-grey-500 ml-2">
                @L["BrowseDescription"]
            </small>
            <div class="pl-2 mt-6">
                <SteamWorkshopLink Text="@L["AllFlagrumMods"]" Tags="@(new[] {"Flagrum"})"/>
            </div>
            <SteamWorkshopLinkGroup Title="@L["OutfitMods"]" DefaultExpanded="true">
                @foreach (var outfit in Enum.GetValues<OutfitSoloTarget>())
                {
                    <SteamWorkshopLink Text=@L[outfit.ToString()] Tags="@(new[] {"Outfit", outfit.ToString()})"/>
                }
            </SteamWorkshopLinkGroup>
            <SteamWorkshopLinkGroup Title="@L["WeaponMods"]" DefaultExpanded="true">
                @foreach (var weapon in Enum.GetValues<WeaponSoloTarget>())
                {
                    <SteamWorkshopLink Text=@L[weapon.ToString()] Tags="@(new[] {"Weapon", weapon.ToString()})"/>
                }
            </SteamWorkshopLinkGroup>
            <SteamWorkshopLinkGroup Title="@L["ModelReplacements"]">
                @foreach (var replacement in DefaultReplacements)
                {
                    <SteamWorkshopLink Text=@L[replacement.ModmetaName] Tags="@(new[] {"Model Replacement", replacement.Name})"/>
                }
                <SteamWorkshopLink Text=@L["Other"] Tags="@(new[] {"Model Replacement", "Other"})"/>
            </SteamWorkshopLinkGroup>
            <SteamWorkshopLinkGroup Title="@L["ComradesStyles"]">
                @foreach (var style in Enum.GetValues<OutfitMultiTarget>())
                {
                    <SteamWorkshopLink Text=@L[style.ToString()] Tags="@(new[] {"Comrades Style", style.ToString()})"/>
                }
            </SteamWorkshopLinkGroup>
            <SteamWorkshopLinkGroup Title="@L["ComradesWeapons"]">
                @foreach (var weapon in Enum.GetValues<WeaponMultiTarget>())
                {
                    <SteamWorkshopLink Text=@L[weapon.ToString()] Tags="@(new[] {"Comrades Weapon", weapon.ToString()})"/>
                }
            </SteamWorkshopLinkGroup>
        </div>
    </div>
</LoadingView>

@code
{
    [CascadingParameter]
    public MainLayout Parent { get; set; }

    private bool IsLoading { get; set; }

    private Dictionary<int, int> Limits { get; } = new()
    {
        {-1, 50 + 999 + 100 + 100 + 999},
        {(int)BinmodType.Cloth, 50},
        {(int)BinmodType.Character, 999},
        {(int)BinmodType.Weapon, 100},
        {(int)BinmodType.Multi_Weapon, 100},
        {(int)BinmodType.StyleEdit, 999}
    };

    private List<ModelReplacementTarget> DefaultReplacements { get; set; }

    protected override void OnInitialized()
    {
        DefaultReplacements = ModelReplacementPresets.GetDefaultReplacements();
        StateHasChanged();
    }

    private void FilterType(int type)
    {
        AppState.ActiveModTypeFilter = type;
        StateHasChanged();
    }

    private void FilterCategory(int category)
    {
        AppState.ActiveCategoryFilter = category;
        StateHasChanged();
    }
}